//
//  ShiPingViewController.m
//  ProJectB_1
//
//  Created by lanou3g on 16/8/12.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#warning 视频1

#import "ShiPingViewController.h"
#import "ShiPinModelU.h"
#import "AFNetworking.h"
#import "MJRefresh.h"

#import "shiPinTableViewCell.h"

typedef enum{
    MYTopicTypeAll =1,
    MYTopicTypePicture =10,
    MYTopicTypeWord =29,
    MYTopicTypeVideo =41,
    MYTopicTyperideo =31
}MYTopicType;

@interface ShiPingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *shipinTableView;

@property (nonatomic,strong) ShiPinModelU *shiPinModelU;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) MYTopicType type;

@property (nonatomic,strong) NSMutableDictionary *params;

@property (nonatomic,strong) NSString *maxtime;
//当前页码
@property (nonatomic,assign)NSInteger page;
@end

@implementation ShiPingViewController

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.type = 41;
    
    
    [self viewLoad];
    
    [self setUpRefresh];

    
    
}

- (void)viewLoad {
    UITableView *shipinTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    [shipinTableView registerClass:[shiPinTableViewCell class] forCellReuseIdentifier:@"shipinTableViewCell"];
    shipinTableView.delegate = self;
    shipinTableView.dataSource = self;
    self.shipinTableView = shipinTableView;
    [self.view addSubview:self.shipinTableView];
    CGFloat top = [UIScreen mainScreen].bounds.size.height / 100 * 3;
    CGFloat botton = [UIScreen mainScreen].bounds.size.height / 100 * 3;
    self.shipinTableView.contentInset = UIEdgeInsetsMake(top, 0, botton, 0);
    //设置滚动条的内边距
    self.shipinTableView.scrollIndicatorInsets = self.shipinTableView.contentInset;
    
    self.shipinTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.shipinTableView.backgroundColor = [UIColor clearColor];
}


#pragma 数据请求
- (void)request {
   
}

#pragma mark ---上下
- (void)setUpRefresh {
    // 下拉刷新
    self.shipinTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)];
    // 自动改变透明度
    self.shipinTableView.header.automaticallyChangeAlpha = YES;
    // 一进界面就刷新
    [self.shipinTableView.header beginRefreshing];
    
    // 上拉刷新
    self.shipinTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}

#pragma mark ---获取数据
- (void)loadNewTopics {
    //发送请求给服务器,参数
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"a"] =@"list";
    params[@"c"] =@"data";
    params[@"type"] = @(self.type);
    self.params = params;
    
    __weak typeof(self)weakSelf = self;
    //发送请求
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSLog(@"%@", responseObject);
        
        if (weakSelf.params != params) {
            return ;
        }
        weakSelf.page = 0;
        weakSelf.maxtime = responseObject[@"info"][@"maxtime"];
        
        NSArray *allArray = responseObject[@"list"];
        for (NSDictionary *diction in allArray) {
            ShiPinModelU *model = [[ShiPinModelU alloc]init];
            [model setValuesForKeysWithDictionary:diction];
            [weakSelf.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.shipinTableView reloadData];
            [weakSelf.shipinTableView.header endRefreshing];
            weakSelf.page = 0;
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //上一次请求参数(用于处理最后一次网络请求)
        if (weakSelf.params !=params) return ;
        //结束刷新
        [weakSelf.shipinTableView.header endRefreshing];
    }];
}

#pragma mark--- 获取更多
- (void)loadMoreTopics {
    [self.shipinTableView.header endRefreshing];
    self.page ++;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"a"] =@"list";
    params[@"c"] =@"data";
    params[@"type"] = @(self.type);
    params[@"page"] =@(self.page);
    params[@"maxtime"] =self.maxtime;
    self.params = params;
    __weak typeof(self) weakSelf = self;
    //发送请求
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
                                
        NSLog(@"%@", responseObject);
        
        //上一次请求参数(用于处理最后一次网络请求)
        if (weakSelf.params !=params) return ;
        
        //存储maxtime(每次返回数据都会有maxtime)
        weakSelf.maxtime =responseObject[@"info"][@"maxtime"];
        
        NSArray *allArray = responseObject[@"list"];
        for (NSDictionary *diction in allArray) {
            ShiPinModelU *model = [[ShiPinModelU alloc]init];
            [model setValuesForKeysWithDictionary:diction];
            [weakSelf.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新表格
            [weakSelf.shipinTableView reloadData];
            //结束刷新
            [weakSelf.shipinTableView.footer endRefreshing];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //上一次请求参数(用于处理最后一次网络请求)
        if (weakSelf.params !=params) return ;
        
        //结束刷新
        [weakSelf.shipinTableView.footer endRefreshing];
        //恢复页码
        weakSelf.page --;

    }];
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    shiPinTableViewCell *cell = [self.shipinTableView dequeueReusableCellWithIdentifier:@"shipinTableViewCell" forIndexPath:indexPath];
    
    ShiPinModelU *shiPin = self.dataArray[indexPath.section];
    cell.shiPinModelU = shiPin;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageAction)];
    [cell.image1 addGestureRecognizer:tap];
    
    
    cell.backgroundColor = [UIColor whiteColor];

    return cell;
    
    
}

- (void)imageAction {
    NSLog(@"123");
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    
    ShiPinModelU *model = self.dataArray[indexPath.section];
    NSString *text = model.text;
    
    CGSize retSize = [text boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 20, 10000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;
    
    return retSize.height + 4 + 20 + 5 + 10 + 10 + ([UIScreen mainScreen].bounds.size.width - 20) / 5 * 3 + 10 + 40 + [UIScreen mainScreen].bounds.size.width / 100 * 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    footer.backgroundColor = [UIColor lightGrayColor];
    return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
@end
